/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.drypisy;

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Marcin
 */
public class ResizeDialog extends javax.swing.JDialog {

    private ResizeProcess proc = new ResizeProcess();
    double propWH;
    double propOrg;
    double propID;
    SmallPanel spanel;
    ChangeListener lis1;
    boolean activeLis = true;
    AppControler control;
    
    public ResizeDialog(java.awt.Frame parent, boolean modal, Dimension dim, AppControler ctrl) {
        super(parent, modal);
        initComponents();
        control = ctrl;
        setResizable(false);
        double w = dim.getWidth();
        double h = dim.getHeight();
        propWH = w/h;
        propOrg = w/h;
        double sw;
        double sh;
        if(w > h){
            sw = 250;
            sh = sw/propWH;
            propID = sw/w;
        }else{
            sh = 250;
            sw = sh*propWH;
            propID = sh/h;            
        }
        spanel = new SmallPanel((int)sw + 10, (int)sh + 10);
        panel.setLayout(new GridLayout(1,1));
        panel.add(spanel);
        spinW.setValue((int)w);
        spinH.setValue((int)h);
        pack();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        spinW = new javax.swing.JSpinner();
        spinH = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        boxProp = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        panel = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Skaluj obraz");
        setMaximumSize(new java.awt.Dimension(300, 300));
        setMinimumSize(new java.awt.Dimension(300, 300));

        spinW.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinWStateChanged(evt);
            }
        });

        spinH.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinHStateChanged(evt);
            }
        });

        jLabel1.setText("Szerokość:");

        jLabel2.setText("Wysokość:");

        boxProp.setText("Zachowaj proporcje");
        boxProp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxPropActionPerformed(evt);
            }
        });

        jButton1.setText("Skaluj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        panel.setMinimumSize(new java.awt.Dimension(350, 350));

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 350, Short.MAX_VALUE)
        );

        jButton2.setText("Przywróc proporcje");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(boxProp)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(spinW, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(spinH))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinW, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boxProp)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boxPropActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxPropActionPerformed
        double w = Double.parseDouble(spinW.getValue().toString());
        double h = Double.parseDouble(spinH.getValue().toString());
        propWH = w/h;
    }//GEN-LAST:event_boxPropActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int w = Integer.parseInt(spinW.getValue().toString());
        int h = Integer.parseInt(spinH.getValue().toString());
        proc.setNewSize(w, h);
        control.setProcess(proc);
        control.process();
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void spinHStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinHStateChanged
        try{
            double h = Double.parseDouble(spinH.getValue().toString());
            double w;
            if(boxProp.isSelected() && activeLis){
                w = h*propWH;
                SpinnerNumberModel m = (SpinnerNumberModel)spinW.getModel();
                activeLis = false;
                m.setValue((int)w);
                activeLis = true;
            }else{
                w = Double.parseDouble(spinW.getValue().toString());
            }
            spanel.setSmallRect(w*propID,h*propID);
        }catch(Exception e){
            e.printStackTrace();
        }
    }//GEN-LAST:event_spinHStateChanged

    private void spinWStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinWStateChanged
        try{
            double w = Double.parseDouble(spinW.getValue().toString());
            double h;
            if(boxProp.isSelected() && activeLis){
                h = w/propWH;
                SpinnerNumberModel m = (SpinnerNumberModel)spinH.getModel();
                activeLis = false;
                m.setValue((int)h);
                activeLis = true;
            }else{
                h = Double.parseDouble(spinH.getValue().toString());
            }
            spanel.setSmallRect(w*propID,h*propID);
        }catch(Exception e){
            e.printStackTrace();
        }
    }//GEN-LAST:event_spinWStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        propWH = propOrg;
        activeLis = false;
        double w = Double.parseDouble(spinW.getValue().toString());
        double h;
        h = w/propWH;
        spinH.setValue((int)h);
        activeLis = true;
    }//GEN-LAST:event_jButton2ActionPerformed

    class SmallPanel extends JPanel{

        private int bW;
        private int bH;
        private int sW;
        private int sH;
        
        public void setSmallRect(double w, double h){
            sW = (int)w;
            sH = (int)h;
            repaint();
        }
        
        public SmallPanel(int sw, int sh) {
            bW = sw - 10;
            bH = sh - 10;
            sW = bW;
            sH = bH;
            /*setSize(sw, sh);
            setMinimumSize(new Dimension(sw,sh));
            setMaximumSize(new Dimension(sw,sh));*/
        }
        
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.clearRect(0, 0, getWidth(), getHeight());
            g2d.setStroke(new BasicStroke(2));
            g2d.drawRect(5, 5, bW, bH);
            g2d.drawRect(5, 5, sW, sH);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox boxProp;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel panel;
    private javax.swing.JSpinner spinH;
    private javax.swing.JSpinner spinW;
    // End of variables declaration//GEN-END:variables
}
